/*
 * @Author: ecitlm
 * @Date:   2017-12-04 21:34:09
 * @Last Modified by: ecitlm
 * @Last Modified time: 2018-06-30 17:23:21
 */
'use strict'
let express = require('express')
let app = express()
let ejs = require('ejs')
const myajax = require('../../utils/fetch')
// 这里也可以配置识别HTML
app.engine('ejs', ejs.__express) // 配置识别ejs模板
app.set('view engine', 'ejs') // 设置模板扩展名后缀自动添加
app.set('views', './views/web') // 设置模板路径
app.get('/:id', function (req, res) {
    let id = req.params.id;
    myajax
    .get('/api/news_list/'+id,{})
    .then(function (response) {
        let data = response.data.map(ele=>({title:ele.title,tagid:ele.tag_id,source:ele.source,ctime:new Date(ele.publish_time*1000).toLocaleDateString(),abstract:ele.abstract,imagelist:ele.image_list,imageurl:ele.image_url}));
        res.render('toutiao', {
        title: '新闻列表',
        data: data
      })
    })
    .catch(function (err) {
      console.log(err)
    })
})

app.get('/show/:id',function(req,res){
    let id = req.params.id;
     myajax.get('/api/news_detail/'+id,{}).then(function(response){
         let data = response.data;
         console.log(data);
         res.render('toutiaodetail', {
             title: data.title,
             data: data
         })
     }).catch(function(err){
         console.log(err);
     })
})

module.exports = app
